package com.lizx.staff.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lizx.admin.service.ExpenditureService;
import com.lizx.admin.service.FileService;
import com.lizx.pojo.Employee;
import com.lizx.pojo.Expenditure;
import com.lizx.pojo.File;
import com.lizx.staff.service.EmployeeService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
@RequestMapping("staff/reimbursement")
public class ReimbursementController {

    @Resource
    ExpenditureService expenditureService;
    @Resource
    EmployeeService employeeService;

    @Resource
    FileService fileService;

    @RequestMapping("list")
    public String list(Model model){

        return "staff/reimbursement/list";
    }

    @RequestMapping("error")
    public String error(){
        return "staff/reimbursement/error";
    }

    @RequestMapping("getList/{start}/{end}")
    @ResponseBody
    public Map<String,Object> getList(HttpServletRequest request,@PathVariable int start,@PathVariable int end){
        String openId = (String) request.getSession().getAttribute("openId");

        LambdaQueryWrapper<Expenditure> queryWrapper = new LambdaQueryWrapper<Expenditure>();
        queryWrapper.eq(Expenditure::getOpenid,openId);
        queryWrapper.ne(Expenditure::getIsdel,1);
        queryWrapper.orderByAsc(Expenditure::getStatus);
        queryWrapper.orderByDesc(Expenditure::getUpdateTime);
        IPage<Expenditure> pagedb = new Page<Expenditure>(start,end);
        IPage<Expenditure> pageResult = expenditureService.page(pagedb, queryWrapper);
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("list",pageResult.getRecords());
        map.put("total",pageResult.getTotal());
        return map;
    }

    @RequestMapping("toAdd")
    public String toAdd(){
        return "staff/reimbursement/add";
    }

    @RequestMapping("findById/{id}")
    @ResponseBody
    public Expenditure findById(@PathVariable int id){
        LambdaQueryWrapper<File> queryWarper = new LambdaQueryWrapper<File>();
        queryWarper.eq(File::geteId,id);
        List<File> fileList = fileService.list(queryWarper);
        List<String> imgList = fileList.stream().map(img -> img.getFilePath()).collect(Collectors.toList());
        Expenditure expenditure = expenditureService.getById(id);
        expenditure.setImgList(imgList);
        return expenditure;
    }

    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public void saveOrUpdate(HttpServletRequest request, @RequestBody Expenditure expenditure){
        String openId = (String) request.getSession().getAttribute("openId");
        LambdaQueryWrapper<Employee> queryWapper = new LambdaQueryWrapper<Employee>();
        queryWapper.eq(Employee::getOpenid,openId);
        Employee employee = employeeService.getOne(queryWapper);
        expenditure.setOpenid(employee.getOpenid());
        expenditure.setUname(employee.getName());
        expenditure.setAid(employee.getAid());
        expenditure.setAname(employee.getAname());
        expenditure.setDataArchiving(0);
        expenditure.setStatus("0");
        expenditure.setUpdateTime(new Date());
        expenditureService.saveOrUpdate(expenditure);

        LambdaQueryWrapper<File> queryWarper = new LambdaQueryWrapper<File>();
        queryWarper.eq(File::geteId,expenditure.getId());
        fileService.remove(queryWarper);
        for (String img : expenditure.getImgList()) {
            File file = new File();
            file.setFilePath(img);
            file.seteId(expenditure.getId());
            fileService.save(file);
        }
    }




}
